package com.dongdongshop.service.impl;

import com.dongdongshop.admin.model.TbSpecification;
import com.dongdongshop.admin.model.TbSpecificationExample;
import com.dongdongshop.admin.model.TbSpecificationOption;
import com.dongdongshop.admin.model.TbSpecificationOptionExample;
import com.dongdongshop.admin.vo.SpecOptionVO;
import com.dongdongshop.admin.vo.SpecVO;
import com.dongdongshop.em.PageRequest;
import com.dongdongshop.em.PageResult;
import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.service.SpecService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SpecServiceImpl implements SpecService {

    @Autowired
    private TbSpecificationMapper specMapper;
    @Autowired
    private TbSpecificationOptionMapper specOptionMapper;

    @Override
    public PageResult specList(PageRequest<SpecVO> specVO) {
        SpecVO spec = specVO.getParms();
        TbSpecificationExample example = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(spec.getSpecName())){
            criteria.andSpecNameLike("%"+spec.getSpecName()+"%");
        }
        PageHelper.startPage(specVO.getPageNum(),specVO.getPageSize());
        List<TbSpecification> tbSpecifications = specMapper.selectByExample(example);
        PageInfo info = new PageInfo<>(tbSpecifications);
        List<SpecVO> collect1 = tbSpecifications.stream().map(i -> {
            SpecVO sp = new SpecVO();
            BeanUtils.copyProperties(i, sp);
            return sp;
        }).collect(Collectors.toList());
        return new PageResult(specVO.getPageNum(),specVO.getPageSize(),info.getTotal(),collect1,3);
    }

    @Override
    public void add(SpecVO specVO) {
        try {
            TbSpecification specification = new TbSpecification();
            BeanUtils.copyProperties(specVO, specification);
            specMapper.inserAndId(specification);
            Long id = specification.getId();
            //增加规格选项
            List<SpecOptionVO> optionVO = specVO.getList();
            List<TbSpecificationOption> optionList = optionVO.stream().map(o -> {
                TbSpecificationOption aa = new TbSpecificationOption();
                BeanUtils.copyProperties(o, aa);
                return aa;
            }).collect(Collectors.toList());
            specOptionMapper.insertSpec(optionList,id);
        }catch (Exception e) {
            throw e;
        }

    }

    @Override
    public SpecVO getSpecById(Long id) {
        TbSpecification tbSpecification = specMapper.selectByPrimaryKey(id);
        if (tbSpecification == null) {
            return null;
        }
        SpecVO specVO = new SpecVO();
        BeanUtils.copyProperties(tbSpecification, specVO);
        //根据规格id查询
        TbSpecificationOptionExample e = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = e.createCriteria();
        criteria.andSpecIdEqualTo(tbSpecification.getId());
        List<TbSpecificationOption> tbSpecificationOptions = specOptionMapper.selectByExample(e);
        List<SpecOptionVO> collect = tbSpecificationOptions.stream().map(specVO1 -> {
            SpecOptionVO vo = new SpecOptionVO();
            BeanUtils.copyProperties(specVO1, vo);
            return vo;
        }).collect(Collectors.toList());
        specVO.setList(collect);
        return specVO;
    }

    @Override
    public void update(SpecVO specVO) {
        try {
            TbSpecification specification = new TbSpecification();
            BeanUtils.copyProperties(specVO, specification);
            specMapper.updateByPrimaryKey(specification);
            Long id = specification.getId();
            TbSpecificationOptionExample e = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = e.createCriteria();
            criteria.andSpecIdEqualTo(id);
            specOptionMapper.deleteByExample(e);
            //增加规格选项
            List<SpecOptionVO> optionVO = specVO.getList();
            List<TbSpecificationOption> optionList = optionVO.stream().map(o -> {
                TbSpecificationOption aa = new TbSpecificationOption();
                BeanUtils.copyProperties(o, aa);
                return aa;
            }).collect(Collectors.toList());
            specOptionMapper.insertSpec(optionList,id);
        }catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void deleteSpec(Long[] ids) {
        try {
            TbSpecificationExample example = new TbSpecificationExample();
            TbSpecificationExample.Criteria criteria = example.createCriteria();
            criteria.andIdIn(Arrays.asList(ids));
            TbSpecificationOptionExample e = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria c = e.createCriteria();
            c.andSpecIdIn(Arrays.asList(ids));
            specOptionMapper.deleteByExample(e);
            specMapper.deleteByExample(example);
        }catch (Exception e) {
            throw e;
        }
    }
}
